package com.baofu.paycore.biz.tools;

import com.baofu.paycore.common.enums.UpdateUser;
import com.baofu.paycore.common.utils.DateUtil;
import com.baofu.paycore.common.utils.IPHelper;
import com.baofu.paycore.dal.model.ErrorLogDO;
import com.baofu.paycore.service.facade.enums.RequestSystemEnum;
import com.system.commons.utils.IPUtil;
import com.system.dispatch.dal.model.BizCmdDO;
import com.system.dispatch.enums.CmdStatusEnums;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.MDC;
import org.slf4j.helpers.SystemMarker;

import java.util.Date;

/**
 * 组装参数
 * <p>
 *     1 命令创建
 *     2
 * </p>
 * User: LZQ Date: 2016/06/28  Version: 1.0
 */
public class BuildModel {
    /**
     * 命令状态未执行
     */
    public final static String BASE_CMD_N_STATUS = "n";
    /**
     * dispatch 命令创建
     *
     * @param bizId     bizId
     * @param bizType   bizType
     */
    public static BizCmdDO buildBizCmd(String bizId, String bizType, Date nextExeTime, int maxRetries, String tradeLogId) {
        BizCmdDO bizCmdDO = new BizCmdDO();
        bizCmdDO.setBizId(bizId);
        bizCmdDO.setBizType(bizType);
        bizCmdDO.setEnvTag(RequestSystemEnum.PAYCORE.getCode());
        bizCmdDO.setServerIP(IPUtil.getLocalIP());
        bizCmdDO.setStatus(CmdStatusEnums.Initial.getCode());
        bizCmdDO.setRetryTimes(1L);
        bizCmdDO.setMaxRetryTimes((long)maxRetries);
        bizCmdDO.setCreatedBy(UpdateUser.SYS.getUserName());
        bizCmdDO.setUpdatedBy(UpdateUser.SYS.getUserName());
        bizCmdDO.setNextExeTime(nextExeTime);
        bizCmdDO.setEnableEndDate(DateUtil.plusDays(1));
        bizCmdDO.setTraceLogId(StringUtils.isEmpty(tradeLogId) ? MDC.get(SystemMarker.TRACE_LOG_ID) : tradeLogId);

        return bizCmdDO;
    }

    /**
     * 差错日志记录创建
     *
     * @param payNo         支付单号
     * @param fundDetailNo  资金单号
     * @param errorType     差错类型
     * @param serviceCode   服务类型
     * @param errorCode     差错编码
     * @param errorMsg      差错描述
     * @param remark        备注
     * @return              差错日志对象
     */
    public static ErrorLogDO buildErrorLog(String payNo, String fundDetailNo, String errorType, String serviceCode,
                                           String errorCode, String errorMsg, String remark){
        ErrorLogDO errorLogDO = new ErrorLogDO();
        errorLogDO.setPayNo(payNo);
        errorLogDO.setFundDetailNo(fundDetailNo);
        errorLogDO.setServiceCode(serviceCode);
        errorLogDO.setErrorType(errorType);
        errorLogDO.setErrorCode(errorCode);
        errorLogDO.setErrorMsg(errorMsg);

        errorLogDO.setMachineIp(IPHelper.getLocalIP());
        errorLogDO.setTransLogId(MDC.get(SystemMarker.TRACE_LOG_ID));
        errorLogDO.setRemark(remark);

        return errorLogDO;
    }

}
